<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("Writer.php");
//Includes
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("components4phpfull/jtpagecontrol.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class details_emission2 extends Page
{
       public $lblmodel = null;
       public $lblmake = null;
       public $Label43 = null;
       public $Label38 = null;
       public $lblyear = null;
       public $lblplate = null;
       public $lblEmpName = null;
       public $lblEmpId = null;
       public $JTJavaScript1 = null;
       public $JTPlatinumVehicleExpense = null;
       public $JTExpenseHidden = null;
       public $edtSearchJournal = null;
       public $btnSearchJournal = null;
       public $btnSearchEm = null;
       public $edtSearchEmission = null;
       public $Label48 = null;
       public $lblVeh = null;
       public $btnExport = null;
       public $btnExportIncident = null;
       public $JTEmissionHidden = null;
       public $JTJSEmission = null;
       public $imgBack = null;
       public $Image1 = null;
       public $JTEmission = null;
       public $JTSpecification = null;
       public $btnSearchSpec = null;
       public $imgSpecs = null;
       public $edtSearchSpec = null;
       public $hiddenId = null;
       public $imageAddSpecification = null;
       public $imageAddJournal = null;
       public $imageAddEmission = null;
       public $JTPageControl1 = null;
       public $JTSiteTheme1 = null;

       function btnExportJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExport'].src="image/excel.png"
       <?php

       }

       function btnExportJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExport'].src="image/excel2.png"
       <?php

       }

       function btnExportIncidentJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExportIncident'].src="image/excel.png"
       <?php

       }

       function btnExportIncidentJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExportIncident'].src="image/excel2.png"
       <?php

       }

       function btnExportIncidentClick($sender, $params)
       {
       $this->JTExpenseHidden->exportGridToXLSDownload('expense_list.xls');


       }




       function btnExportClick($sender, $params)
       {
       $this->JTEmissionHidden->exportGridToXLSDownload('emission_expense.xls');


       }

       function JTEmissionHiddenShow($sender, $params)
       {

       $result_vin = mysql_query("SELECT * FROM vehicle_tbl WHERE vehicle_id='" .$this->hiddenId->getValue(). "'");
       $row_selected = mysql_fetch_array($result_vin);


       $arr = array();


       $arr[] = array(
                        '1' => 'Plate Num:'.' '.$row_selected['veh_num'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );

       $arr[] = array(
                        '1' => 'Year:'.' '.$row_selected['year'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );

       $arr[] = array(
                        '1' => 'Make:'.' '.$row_selected['manufacturer_name'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );

       $arr[] = array(
                        '1' => 'Model:'.' '.$row_selected['model_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );

       $arr[] = array(
                        '1' => 'Type/Style:'.' '.$row_selected['style_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );

       $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );


       $query0 = "select * from veh_emission_tbl where vehicle_id='".$this->hiddenId->getValue()."'and (inspection_date like '%".$this->edtSearchEmission->Text."%'
        or validity_date like '%".$this->edtSearchEmission->Text."%'
        or inspector like '%".$this->edtSearchEmission->Text."%') order by emission_id ASC";
       $result0 = mysql_query($query0)or die(mysql_error());


       $arr[] = array(
                        '1' => 'EMISSION REPORT',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );


       $arr[] = array(
                        '1' => 'Test Date',
                        '2' => 'Valid Thru',
                        '3' => 'Inspector',
                        '4' => 'Account',
                        '5' => 'Expense'
                        );


       while($row = mysql_fetch_array($result0))
       {
         $emission = $row['emission_id'];
         $test_date = "<a href=veh_emission_edit.php?veh=".$this->hiddenId->getValue()."&emission=".$emission." title='edit emission'>".$row['inspection_date']."</a>";
         $tools_emission = "<img onclick=js_deleteEmission('".$this->hiddenId->getValue()."','".$emission."'); src='image/delete_orange.jpg' title='delete'>";
         //<a href=veh_emission_delete.php?veh=".$this->hiddenId->getValue()."&emission=".$emission."><img src='image/delete_orange.jpg' title='delete' no border></a>";

         $arr[] = array(
                        '1' => $row['inspection_date'],
                        '2' => $row['validity_date'],
                        '3' => $row['inspector'],
                        '4' => $row['account'],
                        '5' => $row['test_fee']
                        );

        }
        $total = mysql_query("SELECT SUM(test_fee) as sum_fee FROM veh_emission_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
        $row_total = mysql_fetch_array($total);

        $arr[] = array(
                        '1' => TOTAL,
                        '5' => $row_total['sum_fee'],
                        );





        $this->JTEmissionHidden->setCellData($arr);


       }



       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function imageAddSpecificationJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddSpecification'].src="image/add_new.png"
       <?php

       }

       function imageAddSpecificationJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddSpecification'].src="image/add_new_blue.png"
       <?php

       }

       function imageAddEmissionJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddEmission'].src="image/add_new.png"
       <?php

       }

       function imageAddEmissionJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddEmission'].src="image/add_new_blue.png"
       <?php

       }

       function imageAddJournalJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddJournal'].src="image/add_new.png"
       <?php

       }

       function imageAddJournalJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddJournal'].src="image/add_new_blue.png"
       <?php

       }

       function btnSearchJournalClick($sender, $params)
       {
       redirect('details_emission2.php?veh='.$this->hiddenId->getValue().'&layer=1');
       }

       function btnSearchSpecClick($sender, $params)
       {
       redirect('details_emission.php?veh='.$this->hiddenId->getValue().'&layer=2&search_spec='.$this->edtSearchSpec->getText());
       }

       function edtSearchSpecJSClick($sender, $params)
       {
       ?>
       //Add your javascript code here
       document.getElementById('edtSearchSpec').value = '';
       <?php

       }

       function edtSearchJournalJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('edtSearchJournal').value = '';
       <?php

       }

       function edtSearchEmissionJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('edtSearchEmission').value = '';
       <?php

       }


       function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }



       function JTPlatinumVehicleExpenseShow($sender, $params)
       {
       $veh_id = $this->hiddenId->getValue();
       $query1 = "select * from veh_expense_tbl where vehicle_id='".$veh_id."' and (date like '%".$this->edtSearchJournal->Text."%'
        or description like '%".$this->edtSearchJournal->Text."%'
        or type like '%".$this->edtSearchJournal->Text."%'
        or vendor like '%".$this->edtSearchJournal->Text."%'
        or comments like '%".$this->edtSearchJournal->Text."%'
        or amount like '%".$this->edtSearchJournal->Text."%') order by date DESC";
       $result = mysql_query($query1)or die(mysql_error());

       $arr = array();
       while($row = mysql_fetch_array($result))
       {

       $date = "<a href=veh_expense_edit.php?veh=".$veh_id."&expense=".$row['payment_id']." title='edit expense'>".$row['date']."</a>";
       $tools = "<img onclick=js_deleteExpense('".$veh_id."','".$row['payment_id']."','".$row['route_payment_id']."'); src='image/delete.png' title='delete' style='cursor:pointer'>";

       $amt = $row['amount'];
       $camt = $this->commafy($amt);
       $div_camt = "<div style='text-align:right'>".$camt."</div>";

       if($_SESSION['access_vehicle']==1)
       {
       $arr[] = array(
                        'Date' => $date,
                        //'Description' => $row['description'],
                        'Expense Type' => $row['type'],
                        'Vendor' => $row['vendor'],
                        'Description' => $row['comments'],
                        'Amount' => $div_camt
                        );
       }
       if($_SESSION['access_vehicle']==2)
       {
       $arr[] = array(
                        'Date' => $date,
                        //'Description' => $row['description'],
                        'Expense Type' => $row['type'],
                        'Vendor' => $row['vendor'],
                        'Description' => $row['comments'],
                        'Amount' => $div_camt,
                        'Tools' => $tools
                        );
       }

       }

      $total_amount = mysql_query("SELECT SUM(amount) as sum_amount FROM veh_expense_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
      $row_amount = mysql_fetch_array($total_amount);

      $tot = $row_amount['sum_amount'];
      $ctot = $this->commafy($tot);
      //$align = "<table><tr><td style='text-align:right'>".$ctot."</td></tr></table>";
      $div_ctot = "<div style='text-align:right'>".$ctot."</div>";
      $align = "<font style='text-align:right'>".$ctot."</font>";
      $ctot_tbl = "<table width = '100%'><tr><td style='text-align:right'>".$ctot."</td></tr></table>";
      //"<table width = '100%'><tr><td align = 'right'>".$ctot."</td></tr></table>"

      $arr[] = array(
                        'Description' => TOTAL,
                        'Amount' => $div_ctot
                        );

       $this->JTPlatinumVehicleExpense->setCellData($arr);

       }


       function JTExpenseHiddenShow($sender, $params)
       {
       $veh_id = $this->hiddenId->getValue();

       $query_veh = "select * from vehicle_tbl where vehicle_id='".$veh_id."'";
       $result_veh = mysql_query($query_veh)or die(mysql_error());
       $row_veh = mysql_fetch_array($result_veh);

       $query_exp = "select * from veh_expense_tbl where vehicle_id='".$veh_id."' order by date DESC";
       $result_exp = mysql_query($query_exp)or die(mysql_error());
       //die($query_exp);
       $arr_report = array();


       $arr_report[] = array(
                        '1' => 'Plate Num:'.' '.$row_veh['veh_num'],
                        '2'=> ' ',
                        '3' => ' '
                    );

       $arr_report[] = array(
                        '1' => 'Year:'.' '.$row_veh['year'],
                        '2'=>' ',
                        '3' => ' '
                    );

       $arr_report[] = array(
                        '1' => 'Make:'.' '.$row_veh['manufacturer_name'],
                        '2'=>' ',
                        '3' => ' '
                    );

       $arr_report[] = array(
                        '1' => 'Model:'.' '.$row_veh['model_name'],
                        '2'=> ' ',
                        '3' => ' '
                    );

       $arr_report[] = array(
                        '1' => 'Type/Style:'.' '.$row_veh['style_name'],
                        '2'=> ' ',
                        '3' => ' '
                    );

       $arr_report[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' '
                    );


       $arr_report[] = array(
                        '1' => 'OTHER EXPENSES REPORT',
                        '2'=>' ',
                        '3' => ' '
                    );

      $arr_report[] = array(
      '1' => 'Date',
      '2' => 'Expense Type',
      '3' => 'Amount'
      );




       while($row_exp = mysql_fetch_array($result_exp))
       {

       //$date = "<a href=veh_expense_edit.php?veh=".$veh_id."&expense=".$row['payment_id']." title='edit expense'>".$row['date']."</a>";
       //$tools = "<img onclick=js_deleteExpense('".$veh_id."','".$row['payment_id']."'); src='image/delete_orange.jpg' title='delete'>";

       $arr_report[] = array(
                        '1' => $row_exp['date'],
                        '2' => $row_exp['type'],
                        '3' => $row_exp['amount']
                        );
       }

      $total_amount = mysql_query("SELECT SUM(amount) as sum_amount FROM veh_expense_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
      $row_amount = mysql_fetch_array($total_amount);


      $arr_report[] = array(
                        '2' => TOTAL,
                        '3' => $row_amount['sum_amount']
                        );

       $this->JTExpenseHidden->setCellData($arr_report);


       }




       function details_emission2Create($sender, $params)
       {
        if($_SESSION['access_vehicle']==1)
        {
         $this->imageAddEmission->setVisible(false);
         $this->imageAddJournal->setVisible(false);
         $this->btnExport->Left = 930;
         $this->btnExportIncident->Left = 930;
        }

        if($_SESSION['access_vehicle']==2)
        {
         $this->imageAddEmission->setVisible(true);
         $this->imageAddJournal->setVisible(true);
         $this->btnExport->Left = 826;
         $this->btnExportIncident->Left = 826;
        }

        $_SESSION['company_id'];
        $id = $_GET['veh'];
        if($id!=null || $id!='')
        $this->hiddenId->setValue($id);

        $this->JTPageControl1->TabIndex = $_GET['layer'];

        //$this->JTEmission_header_0_caption->align="center";

        $this->imgBack->setLink('vehicle_list.php?company_id='.$_SESSION['company_id']);
        $this->imageAddEmission->setLink('veh_emission_create.php?veh='.$this->hiddenId->getValue());
        $this->imageAddSpecification->setLink('veh_specification_create.php?veh='.$this->hiddenId->getValue());
        //$this->imageAddJournal->setLink('veh_incident_create.php?veh='.$this->hiddenId->getValue());
        $this->imageAddJournal->setLink('veh_expense_create.php?veh='.$this->hiddenId->getValue());


        $result_vin = mysql_query("SELECT * FROM vehicle_tbl WHERE vehicle_id='" .$this->hiddenId->getValue(). "'");
        $row_selected = mysql_fetch_array($result_vin);
        $label = $row_selected['VIN'];
        $this->lblVeh->Caption = $label;
        $this->lblyear->Caption = $row_selected['year'];
        $this->lblmake->Caption = $row_selected['manufacturer_name'];
        $this->lblmodel->Caption = $row_selected['model_name'];
        $this->lblplate->Caption = $row_selected['veh_num'];

       }

       function JTSpecificationShow($sender, $params)
       {
       $this->edtSearchSpec->setText($_GET['search_spec']);
       $query2 = "select * from veh_specification_tbl where vehicle_id='".$this->hiddenId->getValue()."' and (description like '%".$this->edtSearchSpec->Text."%'
        or quantity like '%".$this->edtSearchSpec->Text."%') order by specification_id ASC";
       $result2 = mysql_query($query2)or die(mysql_error());

       $arr = array();
       while($row_spec = mysql_fetch_array($result2))
       {

       $spec_id = $row_spec['specification_id'];
       $spec_edit = "<a href=veh_specification_edit.php?veh=".$this->hiddenId->getValue()."&specification=".$spec_id." title='edit specification'>".$row_spec['description']."</a>";
       $tools_spec = "<img onclick=js_deleteSpec('".$this->hiddenId->getValue()."','".$spec_id."'); src='image/delete.png' title='delete'>";
       //"<a href=veh_specification_delete.php?veh=".$this->hiddenId->getValue()."&specification=".$spec_id."><img src='image/delete_orange.jpg' title='delete' no border></a>";

       if($_SESSION['isAdmin']==1)
       {
       $arr[] = array(
                        'Description' => $spec_edit,
                        'Value/Qty' => $row_spec['quantity'],
                        'Tools' => $tools_spec
                        );
       }

       if($_SESSION['isAdmin']!=1)
       {
       $arr[] = array(
                        'Description' => $spec_edit,
                        'Value/Qty' => $row_spec['quantity']
                        );
       }

       }

       $this->JTSpecification->setCellData($arr);


       }

       function JTEmissionShow($sender, $params)
       {
       $query0 = "select * from veh_emission_tbl where vehicle_id='".$this->hiddenId->getValue()."'and (inspection_date like '%".$this->edtSearchEmission->Text."%'
        or validity_date like '%".$this->edtSearchEmission->Text."%'
        or inspector like '%".$this->edtSearchEmission->Text."%'
        or account like '%".$this->edtSearchEmission->Text."%'
        or test_fee like '%".$this->edtSearchEmission->Text."%') order by emission_id ASC";
       $result0 = mysql_query($query0)or die(mysql_error());

       $arr = array();
       while($row = mysql_fetch_array($result0))
       {
         $emission = $row['emission_id'];
         $test_date = "<a href=veh_emission_edit.php?veh=".$this->hiddenId->getValue()."&emission=".$emission." title='edit emission'>".$row['inspection_date']."</a>";
         $tools_emission = "<img onclick=js_deleteEmission('".$this->hiddenId->getValue()."','".$emission."'); src='image/delete.png' title='delete' style='cursor:pointer'>";
         //<a href=veh_emission_delete.php?veh=".$this->hiddenId->getValue()."&emission=".$emission."><img src='image/delete_orange.jpg' title='delete' no border></a>";

         $tf = $row['test_fee'];
         $test_f = $this->commafy($tf);


         if($_SESSION['access_vehicle']==1)
         {
         $arr[] = array(
                        'Test Date' => $test_date,
                        'Valid Thru' => $row['validity_date'],
                        'Inspector' => $row['inspector'],
                        'Account' => $row['account'],
                        'Expense' => "<p style align='right'>".$test_f."</p>"
                        );
         }
         if($_SESSION['access_vehicle']==2)
         {
         $arr[] = array(
                        'Test Date' => $test_date,
                        'Valid Thru' => $row['validity_date'],
                        'Inspector' => $row['inspector'],
                        'Account' => $row['account'],
                        'Expense' => "<p style align='right'>".$test_f."</p>",
                        'Tools' => $tools_emission
                        );
         }

        }
        $total = mysql_query("SELECT SUM(test_fee) as sum_fee FROM veh_emission_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
        $row_total = mysql_fetch_array($total);

        $tot = $row_total['sum_fee'];
        $sum_tot = $this->commafy($tot);

        $arr[] = array(
                        'Test Date' => TOTAL,
                        'Expense' => "<p style align='right'>".$sum_tot."</p>"
                        );





        $this->JTEmission->setCellData($arr);


       }

}

global $application;

global $details_emission2;

//Creates the form
$details_emission2=new details_emission2($application);

//Read from resource file
$details_emission2->loadResource(__FILE__);

//Shows the form
$details_emission2->show();

?>